﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using fcwms.Common;
using System.Text;

public partial class admin_login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {

        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string userName = txtUserName.Text.Trim();
        //string userPwd = DESEncrypt.Encrypt(txtUserPwd.Text.Trim());
        string userPwd = txtUserPwd.Text.Trim();

        //string code = txtCode.Text.Trim();
        if (userName.Equals("") || userPwd.Equals(""))
        {
            lblTip.Visible = true;
            lblTip.Text = "请输入用户名或密码";
            return;
        }

        //else if (code.Equals(""))
        //{
        //    lblTip.Visible = true;
        //    //lblTip.Text = "请输入验证码";
        //    lblTip.Text = "请输入问题答案";
        //    return;
        //}
        //else if (Session["vcode"].ToString() == null)
        //{
        //    lblTip.Visible = true;
        //    lblTip.Text = "系统找不到验证码";
        //    return;
        //}
        //else if (code.ToLower() != Session["vcode"].ToString().ToLower())
        //{
        //    lblTip.Visible = true;
        //    lblTip.Text = "问题答案输入不正确";
        //    return;
        //}

        //fcwms.BLL.dt_users bll = new fcwms.BLL.dt_users();
        //fcwms.Model.dt_users model = bll.GetModel(userName, userPwd);
        //if (model == null)
        //{
        //    lblTip.Visible = true;
        //    lblTip.Text = "用户名或密码有误";
        //    return;
        //}

        //StringBuilder strSql = new StringBuilder();
        //strSql.Append("select top 1 role_id from dt_users ");
        //strSql.Append("where user_id = @user_id ");
        //SqlParameter[] parm = new SqlParameter[] { 
        //        new SqlParameter("@user_id", userName)};
        //string title = Convert.ToString(DbHelperSQL.GetSingle(strSql.ToString(), parm));
        //if (string.IsNullOrEmpty(title))
        //{
        //    lblTip.Visible = true;
        //    lblTip.Text = "您的账号缺少相应信息，请联系管理员解决";
        //    return;
        //}
        fcwms.BLL.dt_users bll = new fcwms.BLL.dt_users();
        fcwms.Model.dt_users model = bll.GetModel(userName, DESEncrypt.Encrypt(userPwd));
        Session[DTKeys.SESSION_ADMIN_INFO] = model;
        string sel1 = "select * from dt_users where user_id = @user_id and user_pwd = @user_pwd and is_lock=0 ";
        SqlParameter[] parms = new SqlParameter[] { 
                new SqlParameter("@user_id", userName),
                new SqlParameter("@user_pwd", DESEncrypt.Encrypt(userPwd))};
        DataSet ds = DbHelperSQL.Query(sel1, parms);
        if (ds.Tables[0].Rows.Count > 0)
        {
            Session["username"] = ds.Tables[0].Rows[0]["user_id"];
            Session["roleid"] = ds.Tables[0].Rows[0]["role_id"];
            Session["userpwd"] = ds.Tables[0].Rows[0]["user_pwd"];
            Utils.WriteCookie("username", "fcwms", model.user_name);
            Utils.WriteCookie("userpwd", "fcwms", model.user_pwd);
            Response.Redirect("index.aspx");
        }
        else
        {
            lblTip.Visible = true;
            lblTip.Text = "用户名或密码有误";
            return;
        }
    }
}